package cloud

import (
	"CloudInterface/common"
	"go.uber.org/zap"
	"io"
	"strings"
)

// 获取云应用列表的接口
func (CR *CloudClient) GetCloudList(data common.CloudListParam) common.ResultData {
	CR.SetParams(data)
	paramUrl := CR.ReqParams.Encode()
	reqUrl := baseUrl + "/open/v1/app_cloud/list?" + paramUrl

	resp, err := CR.client.Get(reqUrl)
	if err != nil {
		zap.L().Error("GetCloudList接口请求异常:" + err.Error())
	}

	defer resp.Body.Close()

	bytes, _ := io.ReadAll(resp.Body)
	//解析数据到结构体
	cloudList := common.ResultData{
		Data: common.CloudListData{},
	}
	DecodeData(bytes, &cloudList)
	return cloudList
}

// 编辑云应用的接口
func (CR *CloudClient) EditCloudApp(data common.EditAppData) common.ResultData {
	CR.SetParams(data)
	defer CR.ResetPostParam(OriginalPostBody)

	url := baseUrl + "/open/v1/app_cloud/edit"

	resp, err := CR.client.Post(url, "application/x-www-form-urlencoded", strings.NewReader(CR.ReqParams.Encode()))

	if err != nil {
		zap.L().Error("err in EditCloudApp execute : " + err.Error())
	}
	defer resp.Body.Close()
	bytes, _ := io.ReadAll(resp.Body)

	editResp := common.ResultData{Data: common.EditResp{}}
	DecodeData(bytes, &editResp)

	return editResp

}

// 一键创建云应用的接口
func (CR *CloudClient) InsertCloudApp(data common.InsertAppData) common.ResultData {
	//CR.setInsertCloudParam(data)
	CR.SetParams(data)
	defer CR.ResetPostParam(OriginalPostBody)

	url := baseUrl + "/open/v1/app_cloud/add_common"
	resp, err := CR.client.Post(url, "application/x-www-form-urlencoded", strings.NewReader(CR.ReqParams.Encode()))

	if err != nil {
		zap.L().Error("err in InsertCloudApp : " + err.Error())
	}
	defer resp.Body.Close()
	bytes, _ := io.ReadAll(resp.Body)

	InsertResp := common.ResultData{Data: common.InitResp{}}
	DecodeData(bytes, &InsertResp)

	return InsertResp
}

// 禁用启用云应用的接口
func (CR *CloudClient) EnableOrDisAble(data common.AbleCloudData) common.ResultData {
	CR.SetParams(data)
	defer CR.ResetPostParam(OriginalPostBody)

	url := baseUrl + "/open/v1/app_cloud/disable_batch"
	resp, err := CR.client.Post(url, "application/x-www-form-urlencoded", strings.NewReader(CR.ReqParams.Encode()))

	if err != nil {
		zap.L().Error("err in EnableOrDisAble CloudApp err : " + err.Error())
	}
	defer resp.Body.Close()
	bytes, err := io.ReadAll(resp.Body)

	AbleCloudResp := common.ResultData{Data: common.AbleCloudResp{}}
	DecodeData(bytes, &AbleCloudResp)
	return AbleCloudResp
}

// 删除云应用的接口
func (CR *CloudClient) DeleteCloudApp(data common.DeleteAppData) common.ResultData {
	//CR.setDeleteCloudParam(data)
	CR.SetParams(data)
	defer CR.ResetPostParam(OriginalPostBody)

	url := baseUrl + "/open/v1/app_cloud/del_batch"

	resp, err := CR.client.Post(url, "application/x-www-form-urlencoded", strings.NewReader(CR.ReqParams.Encode()))

	if err != nil {
		zap.L().Error("err in DeleteCloudApp err : " + err.Error())
	}
	defer resp.Body.Close()
	bytes, _ := io.ReadAll(resp.Body)
	deleteAppResp := common.ResultData{Data: common.DeleteAppResp{}}
	DecodeData(bytes, &deleteAppResp)
	return deleteAppResp
}
